<?php

namespace app\admin\controller;

use app\common\controller\Backend;

use think\Request;

class Project extends Backend
{
	public function __construct()
	{
	    parent::__construct();
	
	    $this->OrderModel = model('Project.Order');
		
		$this->StatusModel = model('Project.Status');
	
	    $this->UserModel = model('User.User');
	}
	
    public function index()
    {
        // 查询商品表['user','cate']
        
        $Projectlist = model('Project.Project')->order('id desc')->paginate(7,false,['query' => Request::instance()->param()]);
        
        
        return $this->fetch('',[
            
			'Projectlist' => $Projectlist
			
        ]);
    }
	
	// 预约服务详情
	public function order()
	{
	    // 查询商品表['user','cate']
	    
	    $Orderlist = $this->OrderModel->with(['user','address','project'])->order('id desc')->paginate(7,false,['query' => Request::instance()->param()]);
	    
	    
	    return $this->fetch('',[
	        
			'Orderlist' => $Orderlist
			
	    ]);
	}
	
	// 预约状态详情
	public function status()
	{
	    // 查询商品表['user','cate']
	    
	    $Statuslist = $this->StatusModel->order('id desc')->paginate(7,false,['query' => Request::instance()->param()]);
	    
	    
	    return $this->fetch('',[
	        
			'Statuslist' => $Statuslist
			
	    ]);
	}
	
	// 服务售后状态添加
	public function statusadd()
	{
		if($this->request->isPost())
		{
			// 状态昵称
			$name = $this->request->param('name');
			
			// 状态值
			$status = $this->request->param('status');
			
			$StatusList = $this->StatusModel->select();
			
			foreach($StatusList as $key => $val)
			{
				if($val['status'] == $status)
				{
					$this->error('添加失败，该售后值已经存在',url('admin/project/status'),null,1);
				}
			}
			
			// 封装数组
			$data = [
				'name' => $name,
				'status' => $status
			];
			
			// 验证
			$result = $this->StatusModel->validate('common/Project/Status')->save($data);
			
			if($result === FALSE)
			{
				$this->error('添加失败');	
			}else{
				// cookie('LoginUser',null);
				$this->success('添加成功',url('admin/project/status'),null,0);
			}
			
		}
		
		return $this->fetch('',[
			
		]);
	}
	
	// 编辑售后状态
	public function statusedit()
	{
		// 售后id
		$id = $this->request->param('id');
		
		$status = $this->StatusModel->find($id);
		
		if(!$status)
		{
			$this->error('编辑失败，改售后类型不存在');
		}
		
		if($this->request->isPost())
		{
			// 售后昵称
			$name = $this->request->param('name');
			
			// 售后状态
			$status = $this->request->param('status');
			
			// 封装数组
			$data = [
				'id' => $id,
				'name' => $name,
				'status' => $status
			];
			
			// 验证
			$result = $this->StatusModel->isUpdate(true)->save($data);
			
			if($result === FALSE)
			{
				$this->error('编辑失败');	
			}else{
				// cookie('LoginUser',null);
				$this->success('编辑成功',url('admin/project/status'),null,0);
			}
			
		}
		
		return $this->fetch('',[
			
			'status' => $status
			
		]);
	}
	
	// 删除售后状态
	public function delstatus()
	{
		if($this->request->isAjax())
		{
			// id
			$id = $this->request->param('id');
			
			// 批量删除
			$str = $this->request->param('str');
			
			if(!empty($id))
			{
				// 查询
				$Status = $this->StatusModel->find($id);
				
				if(!$Status)
				{
					$this>error('删除失败，该售后转台不存在');
				}
				
				$result = $this->StatusModel->where('id',$id)->delete();
				
				if($result === FALSE)
				{
					$this->error('删除失败');
					
				}else{
					
					$this->success('删除成功','/admin/project/status');
				}
			}
			
			if(!empty($str))
			{
				// 查询
				$Status = $this->StatusModel->whereIn('id',$str)->select();
				
				if(!$Status)
				{
					$this>error('删除失败，该售后状态不存在');
				}
				
				$result = $this->StatusModel->whereIn('id',$str)->delete();
				
				if($result === FALSE)
				{
					
				    $this->error('删除失败');
				
				}else{
									
				    $this->success('删除成功','/admin/project/status');
					
				}
				
			}
			
		}
		
	}
	
	// 编辑预约订单
	public function orderedit()
	{
		// 预约id
		$id = $this->request->param('id');
		
		$StatusList = model('Project.Status')->select();
		
		$order = $this->OrderModel->with(['user','address','project'])->find($id);
		
		// user
		$user = model('User.User')->find($order['userid']);
		
		if(!$user)
		{
			$this->error('编辑失败，该预约用户不存在');
		}
		
		if(!$order)
		{
			$this->error('编辑失败，该预约订单不存在');
		}
				
		if($this->request->isPost())
		{
			// 服务昵称
			$id = $this->request->param('id');
			
			// 价格
			$price = $this->request->param('price');
			
			// 状态
			$status = $this->request->param('status');
			
			// 用户备注
			$content = $this->request->param('content');
			
			// 退款
			if($order['previousstatus'] == 2)
			{
				
				// 开启事务
				$this->UserModel->startTrans();
				$this->OrderModel->startTrans();
				
				// 删除记录,并且把其状态改为-1
				if($status == -1)
				{
					$OrderData = [
						'id' => $order['id'],
						'status' => -1,
						'previousstatus' => -1
					];
					
					$OrderStatus = $this->OrderModel->isUpdate(true)->save($OrderData);
					
					if($OrderStatus === FALSE)
					{
					    $this->error($this->OrderModel->getError());
					}
					
					$price = intval($order['price']);
					
					// 退款用户积分
					$UserData = [
						'id' => $user['id'],
						'point' => bcadd($user['point'],$price),
					];
					
					
					$UserStatus = $this->UserModel->isUpdate(true)->save($UserData);
					
					if($UserStatus === FALSE)
					{
						$this->OrderModel->rollback();
					    $this->error($this->UserModel->getError());
					}
					
					if($OrderStatus === FALSE || $UserStatus === FALSE)
					{
						$this->OrderModel->rollback();
						$this->UserModel->rollback();
						$this->error('撤销失败');
					}else{
						$this->OrderModel->commit();
						$this->UserModel->commit();
						$this->success('撤销成功',url('admin/project/order'),null,0);
					}
					
				}
				
			}else if($order['previousstatus'] != 2){
				// 封装数组
				$data = [
					'id' => $id,
					'price' => $price,
					'status' => $status,
					'content' => $content,
					'previousstatus' => $status
				];
				
				// 修改
				$result = $this->OrderModel->isUpdate(true)->save($data);
				
				if($result === FALSE)
				{
					$this->error('填写失败');	
				}else{
					// cookie('LoginUser',null);
					$this->success('填写成功',url('admin/project/order'),null,0);
				}
				
			}
			
		}
		
		return $this->fetch('',[
			
			'Order' => $order,
			
			'StatusList' => $StatusList
			
		]);
	}
	
	// 恢复删除的预约订单
	public function restore(){
		
		if($this->request->isAjax()){
			
			// 获取启动数据
			$arrstr = $this->request->param('str');
			
			// 字符串转数组
			$arr = explode(',',$arrstr);
			
			$data = [];
			
			// 循环
			foreach($arr as $item){
				$data[] = [
					'id' => $item,
					'deletetime' => null
				];
			}
			
			$result = $this->OrderModel->isUpdate(true)->saveAll($data);
			
			if($result === FALSE)
			{
				$this->error('恢复失败');	
			}else{
				// cookie('LoginUser',null);
				$this->success('恢复成功',url('admin/project/order'),null,0);
			}
			
		}
		
	}
	
	// 软删除
	// 删除商品
	public function delorder()
	{
		if($this->request->isAjax())
		{
			// id
			$id = $this->request->param('id');
			
			// 批量删除
			$str = $this->request->param('str');
			
			if(!empty($id))
			{
				// 查询
				$Order = $this->OrderModel->find($id);
				
				if(!$Order)
				{
					$this>error('删除失败，该预约订单不存在');
				}
				
				$result = $this->OrderModel->destroy($id);
				
				if($result === FALSE)
				{
					$this->error('删除失败');
					
				}else{
					
					$this->success('删除成功','/admin/project/order');
				}
			}
			
			if(!empty($str))
			{
				// 查询
				$Order = $this->OrderModel->whereIn('id',$str)->select();
				
				if(!$Order)
				{
					$this>error('删除失败，该预约订单不存在');
				}
				
				$result = $this->OrderModel->destroy($str);
				
				if($result === FALSE)
				{
					
				    $this->error('删除失败');
				
				}else{
									
				    $this->success('删除成功','/admin/project/order');
					
				}
				
			}
			
		}
		
	}
	
	// 查看删除的预约订单
	public function restorelist(){
		// whereNotNull('belong')  'deletetime', 'is not null'
		$restorelist = $this->OrderModel->onlyTrashed()->paginate(7,false,['query' => Request::instance()->param()]);
		
		return $this->fetch('',[
			
			'restorelist' => $restorelist
			
		]);
		
	}
	
	
	// 服务编辑
	public function projectedit()
	{
		// 服务id
		$id = $this->request->param('id');
		
		$project = model('Project.Project')->find($id);
		
		if(!$project)
		{
			$this->error('编辑失败，改服务类型不存在');
		}
		
		if($this->request->isPost())
		{
			// 服务昵称
			$name = $this->request->param('name');
			
			// 封装数组
			$data = [
				'id' => $id,
				'name' => $name
			];
			
			// 验证
			$result = model('Project.Project')->isUpdate(true)->save($data);
			
			if($result === FALSE)
			{
				$this->error('编辑失败');	
			}else{
				// cookie('LoginUser',null);
				$this->success('编辑成功',url('admin/project/index'),null,0);
			}
			
		}
		
		return $this->fetch('',[
			
			'project' => $project
			
		]);
	}
	
	// 服务添加
	public function projectadd()
	{
		if($this->request->isPost())
		{
			// 服务昵称
			$name = $this->request->param('name');
			
			// 封装数组
			$data = [
				'name' => $name
			];
			
			// 验证
			$result = model('Project.Project')->validate('common/Project/Project')->save($data);
			
			if($result === FALSE)
			{
				$this->error('添加失败');	
			}else{
				// cookie('LoginUser',null);
				$this->success('添加成功',url('admin/project/index'),null,0);
			}
		}
		
		return $this->fetch('',[
			
		]);
		
	}
}
